package com.example.lottery_system.service.impl;

import cn.hutool.core.lang.UUID;
import com.example.lottery_system.common.errcode.ServiceErrorCodeConstant;
import com.example.lottery_system.common.exception.ServiceException;
import com.example.lottery_system.controller.param.PageParam;
import com.example.lottery_system.controller.param.PrizeParam;
import com.example.lottery_system.controller.result.PageResult;
import com.example.lottery_system.dao.DataObject.PrizeDO;
import com.example.lottery_system.dao.Mapper.PrizeMapper;
import com.example.lottery_system.service.PrizeService;
import com.example.lottery_system.service.dto.PrizeListDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PrizeServiceImpl implements PrizeService {



    @Value("${pic.path}")
    private String picPath;

    @Autowired
    PrizeMapper prizeMapper;


    @Override
    public String picUp(MultipartFile multipartFile) {
        log.info("传入的文件的参数:" + multipartFile);
        // 创建目录
        File dir = new File(picPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }


        // 创建索引
//        将 abc.png -> UUID.png
        String originName =  multipartFile.getOriginalFilename();
        assert (originName != null);
        String buffer = originName.substring(originName.lastIndexOf("."));
        String  newFile = UUID.randomUUID() +  buffer;

        // 保存文件
        try {
            multipartFile.transferTo(new File(picPath + "/" + newFile));
        } catch (IOException e) {
            throw new ServiceException(ServiceErrorCodeConstant.PRIZE_PIC_UPLOAD_ERROR);
        }

        return newFile;
    }



    @Override
    public Long createPrizeService(PrizeParam param, MultipartFile picFile) {
//        上传图片
        String picUrl = picUp(picFile);


//        构造奖品数据
        PrizeDO prizeDo = new PrizeDO();
        prizeDo.setName(param.getName());
        prizeDo.setPrice(param.getPrice());
        prizeDo.setDescription(param.getDescription());
        prizeDo.setImageUrl(picUrl);

        // 插入数据库并且返回自身的id
       try {
           prizeMapper.insertPrize(prizeDo);
       } catch (Exception e) {
           throw  new ServiceException(ServiceErrorCodeConstant.PRIZE_INSERT_ERROR);
       }

        return prizeDo.getId();
    }

    @Override
    public PageResult<List<PrizeListDto>> findPrizes(PageParam param) {
        // 查找总页数
        int count = prizeMapper.selectCount();

        // 查找当前页的列表
        int offset = param.offset(param.getCurrentPage(), param.getPageSize());
        List<PrizeDO> prizeDOList = prizeMapper.selectPrizeList(offset,param.getPageSize());

        // 转化奖品数据类型
        List<PrizeListDto> list = new ArrayList<>();
        for (PrizeDO prizeDo: prizeDOList) {
            PrizeListDto prizeListDto = new PrizeListDto(
                    prizeDo.getId(),
                    prizeDo.getName(),
                    prizeDo.getDescription(),
                    prizeDo.getPrice(),
                    prizeDo.getImageUrl()
            );
            list.add(prizeListDto);
        }

//        构造返回类型
        PageResult<List<PrizeListDto>> pageResult = new PageResult<>(count,list);
        return pageResult;
    }

    @Override
    public Boolean deletePrize(Long prizeId) {
        if (null == prizeId) {
            throw new ServiceException(ServiceErrorCodeConstant.PRIZE_DELETE_IS_EMPTY);
        }

        try {
            prizeMapper.deletePrize(prizeId);
            return true;
        } catch (Exception e) {
            throw  new ServiceException(ServiceErrorCodeConstant.PRIZE_DELETE_ERROR);
        }
    }


}
